<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商品分类管理
 */
namespace app\allwin\controller;
use app\allwin\model\AllwinShopCate;
use app\allwin\model\AllwinShop;
use app\allwin\model\AllwinStore;

class ShopCate extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'宝贝专题','url'=>url("allwin/shopCate/index")]]);
    }

    /**
     * 列表
     */
    public function index(){
        $condition = [];
        $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
        $view['lists'] = AllwinShopCate::where($condition)->order('sort desc,id desc')->paginate(20);
        return view()->assign($view);
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'title'             => $this->request->param('title/s'),
                'content'           => $this->request->param('content/s'),
                'picture'           => $this->request->param('picture/s'),
                'types'             => $this->request->param('types/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Category.save');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result =  AllwinShopCate::edit($data);
            if($result){
                return enjson(200,'操作成功',['url' => url('allwin/shopcate/index')]);
            }else{
                return enjson(0,'操作失败');
            }
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'      => $this->request->param('id/s'),
                'title'   => $this->request->param('title/s'),
                'content' => $this->request->param('content/s'),
                'types'   => $this->request->param('types/d'),
                'picture' => $this->request->param('picture/s'),
            ];
            $validate = $this->validate($data,'Category.edit');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result =  AllwinShopCate::edit($data);
            if($result){
                return enjson(200,'操作成功');
            }else{
                return enjson(0,'操作失败');
            }
        }else{
            $view['info'] = AllwinShopCate::where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            if(empty($view['info'])){
                $this->error("404 NOT FOUND");
            }
            return view('edit',$view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Category.sort');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result = AllwinShopCate::update(['sort'=>$data['sort'],'id' => $data['id']]);
            if($result){
                return enjson(200,'操作成功');
            }else{
                return enjson(0,'操作失败');
            }
        }
    }

    //删除
    public function delete(int $id){
        $goods =  AllwinShop::where(['category_id' => $id])->find();
        if($goods){
            return enjson(403,'删除失败,请查看是否包含商品');
        }
        $result = AllwinShopCate::destroy(['id' => $id,'member_miniapp_id' => $this->member_miniapp_id]);
        if($result){
            return enjson(200,'操作成功');
        }else{
            return enjson(403,'删除失败,请查看是否包含商品');
        } 
    }
}